import 'dart:convert';

import 'package:dio/dio.dart' as dio;
import 'package:flutter_axrjc/datasource/db/DBLightApi.dart';
import 'package:flutter_axrjc/datasource/network/bean/UserBean.dart';
import 'package:flutter_axrjc/datasource/network/request/network_service.dart';
import 'package:flutter_axrjc/page/splash/logic.dart';
import 'package:flutter_axrjc/routes.dart';
import 'package:flutter_axrjc/util/Util.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class UserApi {
  static Future<UserBean?> register(
      {required String phone,
      required String yzm,
      required String password}) async {
    var res = await NetworkService.post(
        url: "app/reg",
        map: {"captcha": yzm, "telPhone": phone, "password": password});
    UserBean? user = res.getTypedData((map) => UserBean.fromJson(map));
    await Util.saveUser(user);
    return user;
  }

  static sendCode({required String telPhone}) async {
    await NetworkService.post(url: "app/getCode", map: {"telPhone": telPhone});
  }

  static Future<UserBean?> login(
      {required String phone, required String password}) async {
    var res = await NetworkService.post(
        url: "app/login", map: {"telPhone": phone, "password": password});
    UserBean? user = res.getTypedData((map) => UserBean.fromJson(map));
    await Util.saveUser(user);
    return user;
  }

  static Future<void> logout() async {
    try {
      await Util.saveUser(null);
      await DBApi.deleteAll();
    } catch (e) {
      print(e);
    }
    Get.offAllNamed(MainRoutes.login, arguments: FormType.splash);
  }

  static Future<UserBean?> getUser() async {
    SharedPreferences share = await SharedPreferences.getInstance();
    String? res = share.getString("user");
    if (res == null || res.isEmpty == true) {
      return null;
    }
    return UserBean.fromJson(jsonDecode(res));
  }

  static Future<String> updateAvatar(String path) async {
    var res = await NetworkService.post(
        url: "app/proBaseCustomerUpdateAvatar.do",
        map: {
          "file": await dio.MultipartFile.fromFile(path),
          "customerId": (await getUser())?.customerInfo?.customerId
        },
        useFormData: true);
    var user = await getUser();
    user?.customerInfo?.avatar = "${res.data}";
    await Util.saveUser(user);
    return "${res.data}";
  }

  static updateProfile(Map<String, dynamic> map) async {
    await NetworkService.post(
        url: 'app/proBaseCustomerUpdate.do', map: map, useFormData: false);
  }

  static changePassword({
    required String yzm,
    required String password,
  }) async {
    var user = await getUser();
    await NetworkService.post(
        url: 'app/updateCustomerPwd.do',
        map: {
          "password": password,
          "telPhone": user?.customerInfo?.telPhone,
          "captcha": yzm,
        },
        useFormData: true);
  }

  static Future getCustomerInfo() async {
    var res = await NetworkService.post(
        url: 'app/getCustomerInfo', map: {}, showLoading: false);
    return res.getTypedData((map) => CustomerInfo.fromJson(map));
  }
}
